package generators;

import gamebackend.Block;

import java.util.Random;

public class HorizontalStreakGenerator extends AbstractRandomGenerator {

	public static final int LEFT = 1;
	public static final int RIGHT = 2;
	public static final int BOTH = 3;
	
	private final int my_direction;
	
	public HorizontalStreakGenerator(final Block the_block, Random the_random,
			double the_chance, final int the_direction) {
		super(the_block, the_random, the_chance);
		my_direction = the_direction;
	}
	
	public HorizontalStreakGenerator(Random the_random,
			double the_chance, final int the_direction) {
		this(null, the_random, the_chance, the_direction);
	}

	@Override
	public void generate(Block[][] blocks) {
		if (my_direction == RIGHT || my_direction == BOTH) {
			for (int col = 1; col < blocks[0].length; col++) {
				if (blocks[0][col] == Block.EMPTY_BLOCK
						&& (blocks[0][col - 1].isOccupied() && getRandom().nextDouble() < getChance())) {
					Block block = getBlock();
					if (block == null) {
						block = blocks[0][col - 1];
					}
					blocks[0][col] = block;
				}
			}
		}
		
		if (my_direction == LEFT || my_direction == BOTH) {
			for (int col = blocks[0].length - 2; col >= 0; col--) {
				if (blocks[0][col] == Block.EMPTY_BLOCK
						&& (blocks[0][col + 1].isOccupied() && getRandom().nextDouble() < getChance())) {
					Block block = getBlock();
					if (block == null) {
						block = blocks[0][col + 1];
					}
					blocks[0][col] = block;
				}
			}
		}
		
	}
	

}
